#Generating bootstrapped confidence intervals for Figure 2
beo$total_sq <- beo$total*beo$total

m0 <- lmer(black_vote_pct ~ total + total_sq + pop_thousands + inc_thousands + college_ed + black_pct + 
             TS1970 + pres_elec_year + (1|name), data=beo)

val <- c(seq(0,44,1))
boots <- as.data.frame(rep(NA,length(val)))

simdat <- as.data.frame(val)
names(simdat) <- "total"
simdat$total_sq <- simdat$total^2
simdat$pop_thousands <- median(beo$pop_thousands)
simdat$inc_thousands <- median(beo$inc_thousands)
simdat$college_ed <- median(beo$college_ed)
simdat$black_pct <- median(beo$black_pct)
simdat$TS1970 <- rep(2, nrow(simdat))
simdat$pres_elec_year <- rep(1, nrow(simdat))

set.seed(1337)

for (i in 1:length(val))
{
  b <- bootMer(m0, nsim=1000, 
               FUN=function(x)predict(x, newdata=simdat[i,], re.form=NA), .progress = "txt", parallel="multicore", ncpus = 2)
  boots$pred[i] <- b$t0
  boots$lwr[i] <- quantile(b$t, probs=0.025)
  boots$upr[i] <- quantile(b$t, probs=0.975)
  boots$val[i] <- simdat$total[i]
  
}

write.csv(boots, "./Data/number_cis.csv", row.names=FALSE)

#Generating bootstrapped confidence intervals for Figure 4
beo$total_prop2 <- beo$total_proportion^2
figure3 <- lmer(black_vote_pct ~ total_proportion + total_prop2 + pop_thousands + inc_thousands + college_ed + black_pct + 
                  TS1970 + pres_elec_year + (1|name), data=beo)

val <- c(seq(0,0.4375,0.00875))
boots <- as.data.frame(rep(NA,length(val)))

simdat <- as.data.frame(val)
names(simdat) <- "total_proportion"
simdat$total_prop2 <- simdat$total_proportion^2
simdat$pop_thousands <- median(beo$pop_thousands)
simdat$inc_thousands <- median(beo$inc_thousands)
simdat$college_ed <- median(beo$college_ed)
simdat$black_pct <- median(beo$black_pct)
simdat$TS1970 <- rep(2, nrow(simdat))
simdat$pres_elec_year <- rep(1, nrow(simdat))

for (i in 1:length(val))
{
  b <- bootMer(figure3, nsim=1000, 
               FUN=function(x)predict(x, newdata=simdat[i,], re.form=NA), .progress = "txt", parallel="multicore", ncpus = 4)
  boots$pred[i] <- b$t0
  boots$lwr[i] <- quantile(b$t, probs=0.025)
  boots$upr[i] <- quantile(b$t, probs=0.975)
  boots$val[i] <- simdat$total_proportion[i]
  
}


write.csv(boots, "./Data/proportion_cis.csv", row.names=FALSE)